<?php

/**
 * This is the model class for table "{{region}}".
 *
 * The followings are the available columns in table '{{region}}':
 * @property integer $id
 * @property string $region_name
 * @property string $name_jp  
 * @property string $create_date
 * @property integer $created_by
 * @property string $update_date
 * @property integer $updated_by
 */
class Region extends CActiveRecord
{
    /**
        * Returns the static model of the specified AR class.
        * @param string $className active record class name.
        * @return Language the static model class
        */

    public $keyword;
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
        * @return string the associated database table name
        */
    public function tableName()
    {
        return '{{region}}';
    }

    /**
        * @return array validation rules for model attributes.
        */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('region_name', 'required', 'message' => t ( 'MSG_REQUIRED', 'messages' )),						
            array('region_name, name_jp', 'length', 'max'=>20, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),                
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('keyword, region_name', 'safe', 'on'=>'search'),
            array('keyword', 'length', 'max' => 100, 'tooLong' => t('MSG_STRING_TOO_LONG', 'messages')),
        );
    }	

    /**
        * @return array customized attribute labels (name=>label)
        */
    public function attributeLabels()
    {
        return array(
            'id' => t('LBL_ID', 'region'),
            'region_name' => t('LBL_REGION_NAME', 'region'),	
            'create_date' => t('LBL_CREATE_DATE', 'region'),
        	'name_jp' => t('LBL_NAME_JP', 'region'),
            'created_by' => t('LBL_CREATED_BY', 'region'),
            'update_date' => t('LBL_UPDATE_DATE', 'region'),
            'updated_by' => t('LBL_UPDATED_BY', 'region'),
        );
    }

    /**
        * Retrieves a list of models based on the current search/filter conditions.
        * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
        */
        public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        $this->keyword = trim($this->keyword);
        $criteria = new CDbCriteria;

        $criteria->addSearchCondition('region_name', $this->keyword, true);                                    

        $sort = new CSort;
        $sort->defaultOrder = array('region_name' => CSort::SORT_DESC);
        $sort->attributes = array(
            '*'                
        );

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => LGHelpers::getPageSize($this->count()),
            ),
            'sort' => $sort
        ));
    }   
    
    public function beforeSave() {
	if (parent::beforeSave()) {

	    $current_date = date(LGConstant::DATE_FORMAT_FOR_DATABASE);

	    //Get user is logging
	    $logedInUser = User::model()->findByPk(Yii::app()->user->id);	   

            if ($this->isNewRecord) {
		$this->create_date = $current_date;
		$this->created_by = $logedInUser->username;
	    }
            
	    $this->update_date = $current_date;

	    if (isset($logedInUser)) {
		$this->updated_by = $logedInUser->username;
	    }	    

	    $this->setIsNewRecord(false);
	    return true;
	} else {
	    return false;
	}
    }        
    
    public function getChtmlLanguage(){
        $listData = $this->findAll();
        return CHtml::listData($listData, 'id', 'region_name'); 
    }
}